package de.g18.scavengerhunt.service;

import java.util.List;

import android.os.Message;
import de.g18.scavengerhunt.AppConstants;
import de.g18.scavengerhunt.activity.GameMapActivity;
import de.g18.scavengerhunt.types.User;
import de.g18.scavengerhunt.utils.WebserviceUtil;

/**
 * This threads consumes the positions of all users from the web service
 */
public class ReceivePositionThread implements Runnable {

    private boolean         running       = false;

    private int             sessionId      = 0;

    private GameMapActivity mapActivity    = null;

    private WebserviceUtil  webserviceUtil = null;

    public ReceivePositionThread(int sessionId, GameMapActivity mapActivity) {
        this.sessionId = sessionId;
        this.mapActivity = mapActivity;
        this.webserviceUtil = WebserviceUtil.getInstance();
    }

    @Override
    public void run() {
        running = true;
        List<User> userList = null;
        while (running) {
            userList = webserviceUtil.getActiveUsers(sessionId);

            Message msg = Message.obtain();
            msg.what = AppConstants.UPDATE_POSITION_FROM_SERVICE;
            msg.obj = userList;
            
            mapActivity.onOverlayUpdate(msg);
            try {
                Thread.sleep(AppConstants.POSITION_SEND_THREAD_SLEEP_TIME_IN_MS);
            } catch (InterruptedException e) {
                // ignore
            }
        }
    }

    /**
     * Stopps this thread
     */
    public void stop() {
        running = false;
    }
}
